<?php

namespace App\Logics;


use App\Models\User;
use App\Service\Base\Logic;

class UserLogic extends Logic
{

    // 详情
    public function info($myUid)
    {
        $userDb = new User();
        $uid    = $myUid ?: $this->option()->getData('uid', 0, 'intval');
        $info   = $userDb->isCache(true)->oneFind(['id' => $uid]);

        // 头像
        $domain    = env('STYLE_HOST');
        $oldDomain = env('OLD_HOST');
        if($info['head_img']){
            $info['head_img'] = str_replace($oldDomain, $domain, $info['head_img']);
        }

        return $info;
    }


    // 完善个人资料
    public function profile($myUid)
    {
        $opt  = $this->option();
        $data = [
            'head_img' => $opt->getData('head_img', null, 'trim'),
            'nickname' => $opt->getData('nickname', null, 'trim'),
            'showname' => $opt->getData('showname', null, 'trim'),
            'sex'      => $opt->getData('sex', null, 'trim'),
            'email'    => $opt->getData('email', null, 'trim'),
            // 以下待添加
            //'sign'     => $opt->getData('sign', null, 'trim'),
            //'birthday' => $opt->getData('birthday', null, 'trim'),
            //'mobile'   => $opt->getData('mobile', null, 'trim'),
        ];

        // 头像
        /*if($data['head_img'] && (strpos($data['head_img'], '?imageMogr2/auto-orient/thumbnail/100x100') === false)){
            $data['head_img'] = $this->showQiniuImage($data['head_img'], 100, 100);
        }*/
        /*if($data['birthday']){
            $data['birthday'] = strtotime($data['birthday']);
        }*/

        if($data['head_img']){
            $domain    = env('STYLE_HOST');
            $oldDomain = env('OLD_HOST');
            // 头像域名替换 使用统一域名地址，方便操作
            $data['head_img'] = str_replace($domain, $oldDomain, $data['head_img']);
        }

        // TODO 必须验证手机号 邮箱 不能重复
        $userDb = new User();
        if(!empty($data['mobile'])){
            $has = $userDb->where('mobile', $data['mobile'])->where('id', '<>', $myUid)->first();
            if($has){
                return $this->_returnFail('mobile_has_exist');
            }
        }
        if(!empty($data['email'])){
            $has = $userDb->where('email', $data['email'])->where('id', '<>', $myUid)->first();
            if($has){
                return $this->_returnFail('email_has_exist');
            }
        }

        $data = $this->filterNull($data);
        if(empty($data)){
            return $this->_returnFail('send_data_to_be_modify');
        }

        // 执行修改
        //$result = $userDb->isCache(true)->oneEdit(['id' => $myUid], $data);
        $result = $userDb->where('id', $myUid)->update($data);
        if($result === false){
            return $this->_returnFail($userDb->getError());
        }

        // 删除缓存
        $userDb->deleteCacheShow(['id' => $myUid], $data);

        // 返回数据
        $info = $userDb->isCache(true)->oneFind(['id' => $myUid]);

        return $info;
    }


    // 认证操作


    // 我的认证资料


    // 注销
    public function cancel($myUid)
    {
        $userDb = new User();
        //$result = $userDb->isCache(true)->cancel($myUid);
        $result = $userDb->isCache(true)->oneEdit(['id' => $myUid], ['status' => -1]);
        if($result === false){
            return $this->_returnFail($userDb->getError());
        }

        return $result;
    }


    // 修改密码
    public function password($myUid = 0)
    {
        if(empty($myUid)){
            return $this->_returnFail('login_before_operating');
        }

        $option    = $this->option();
        $password  = $option->getData('password', null, 'trim');
        $password2 = $option->getData('password2', null, 'trim');
        if(empty($password)){
            return $this->_returnFail('enter_the_password');
        }
        if(empty($password2)){
            return $this->_returnFail('enter_the_confirm_password');
        }
        if($password2 != $password){
            return $this->_returnFail('passwords_entered_are_not_the_same');
        }

        // 获取登录的用户信息
        $userDb = new User();
        $where  = ['id' => $myUid];
        $info   = $userDb->isCache(true)->oneFind($where);
        if($info === false){
            return $this->_returnFail($userDb->getError());
        }
        if(empty($info)){
            return $this->_returnFail('user_has_not_exist_or_has_been_disabled');
        }

        // 如果没有 则注册
        // $salt  = Str::random(4);
        $salt   = $info['salt'];
        $_pass  = $userDb->add_md5($password, $salt);  // 密码
        $result = $userDb->where($where)->update(['password' => $_pass]);

        // 缓存操作
        $this->deleteCacheShow($info);

        return $result;
    }


    // 验证登录用户的密码
    public function checkPassword($myUid)
    {
        $option   = $this->option();
        $password = $option->getData('password', null, 'trim');

        // 验证密码
        $userDb = new User();
        $info   = $userDb->isCache(true)->oneFind(['id' => $myUid]);
        $info   = is_object($info) ? $info->toArray() : $info;
        $res    = $userDb->ckeckPassword($password, $info['salt'], $info['password']);
        if(!$res){
            return $this->_returnFail('password_is_wrong');
        }

        return true;
    }

}


